package com.hqj.utils;

import java.io.*;
import java.util.Properties;

/**
 * 文件工具类
 */
public class FileUtil {

    public static boolean createDir(File f){
        boolean b = false;
        if(!f.exists()){
            b = f.mkdirs();
        }else{
            b = true;
        }
        return b;
    }

    public static boolean createFile(File f){
        boolean b = false;
        try{
            if(!f.exists()) {
                b = f.createNewFile();
            }else{
                b = true;
            }
        }catch (IOException e){
            b = false;
            System.out.println("路径错误或不存在!");
        }
        return b;
    }

    /**
     * 生成带副本的文件名称
     * @param f 文件
     * @return
     */
    public static String createFileName(File f){
        if(f.isDirectory()){
            return f.getName();
        }
        String newFileName = "";
        int i = 1;
        while(true){
            String fileName = f.getName();
            int index = fileName.lastIndexOf(".");
            String suffix = fileName.substring(index);
            String prefix = fileName.substring(0,index);//012 - 副本
            if(i==1) {
                prefix += " - 副本";
            }else{
                prefix += "- 副本 ("+i+")";
            }

            newFileName = prefix + suffix;
            File file = new File(f.getParent(),newFileName);
            if(file.exists()){
                i++;
                continue;
            }
            break;
        }

        return newFileName;
    }

    public static void fileInputAndOutput(File in,File out){
        InputStream is = null;
        OutputStream os = null;
        try{

            is = new FileInputStream(in);
            os = new FileOutputStream(out,false);

            int i = 0;
            byte bs[] = new byte[1024];
            while((i=is.read(bs))!=-1){
                os.write(bs,0,i);
            }

            if(os!=null) os.close();
            if(is!=null) is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void fileReaderAndWriter(File reader,File writer){
        FileReader fr = null;
        BufferedReader br = null;
        FileWriter fw = null;
        BufferedWriter bw = null;
        try{
            fr = new FileReader(reader);
            br = new BufferedReader(fr);
            fw = new FileWriter(writer,false);
            bw = new BufferedWriter(fw);

            String $s = "";
            while(($s=br.readLine())!=null){
                bw.write($s);
            }
            bw.flush();

            if(bw!=null) bw.close();
            if(fw!=null) fw.close();
            if(br!=null) br.close();
            if(fr!=null) fr.close();

        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static void outputObject(File f,Object obj){
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try{

            os = new FileOutputStream(f,false);
            oos = new ObjectOutputStream(os);

            oos.writeObject(obj);

            if(oos!=null) oos.close();
            if(os!=null) os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Object inputObject(File f){
        try{
            InputStream is = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(is);

            Object obj = ois.readObject();
            if(ois!=null) ois.close();
            if(is!=null) is.close();
            return obj;
        }catch (EOFException e){
            System.out.println("第一次读取，对象为空");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static void storeProperties(File f, Properties pro){
        try{
            OutputStream os = new FileOutputStream(f,false);

            pro.store(os,null);

            if(os!=null) os.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Properties loadProperties(File f){
        try{
            InputStream is = new FileInputStream(f);
            Properties pro = new Properties();
            pro.load(is);
            if(is!=null) is.close();
            return pro;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
